package com.jy.adscm.operation.mask.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mask.bean.KzMaterial;
import com.jy.adscm.operation.mask.bean.KzMaterialDetail;
import com.jy.adscm.operation.mask.bean.QueryKzMaterialDetail;
import com.jy.adscm.operation.mask.service.KzMaterialDetailService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.method.P;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2020-03-24
 */
@RestController
@RequestMapping("/adscm/kz/kzMaterialDetail")
public class KzMaterialDetailController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    KzMaterialDetailService kzMaterialDetailService;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody KzMaterialDetail kzMaterialDetail, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        kzMaterialDetail.setCreateTime(new Date());
        kzMaterialDetail.setCreator(token.getUser().getId().toString());
        KzMaterialDetail kzMaterialDetailnew = kzMaterialDetailService.insertkzMaterialDetail(kzMaterialDetail);
        kzMaterialDetailService.printCode(kzMaterialDetailnew.getCode());
        return new SimpleCoreResponse("200","新增成功",true);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody KzMaterialDetail kzMaterialDetail, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = kzMaterialDetailService.updateKzMaterialDetail(token,  kzMaterialDetail);
        return new SimpleCoreResponse("200","修改成功",insert);
    }
    @RequestMapping("selectByPid")
    public SimpleCoreResponse selectByPid(@RequestBody QueryKzMaterialDetail queryKzMaterialDetail, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Page<KzMaterialDetail> page = kzMaterialDetailService.selectByPid(queryKzMaterialDetail);
        return new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("getMaxCode")
    public SimpleCoreResponse getMaxCode( HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
         String code  = kzMaterialDetailService.getMaxCode();
        return new SimpleCoreResponse("200","查询成功",code);
    }

    @RequestMapping("outPutMaterial/{code}/{result}")
    public SimpleCoreResponse outPutMaterial( @PathVariable String code, @PathVariable Integer result, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
            Boolean info= kzMaterialDetailService.outPutMaterial(code,result,token);
        return new SimpleCoreResponse("200","查询成功",info);
    }

    
    
}

